package com.dhy;

import com.dhy.util.RabbitmqUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

import static com.dhy.constants.RabbitmqConstants.*;

/**
 * @author 大忽悠
 * @create 2022/5/19 18:50
 */
@Slf4j
public class Publisher implements Runnable {
    @Override
    public void run() {
        try {
            RabbitmqUtil rabbitmqUtil = new RabbitmqUtil("application.yml");
            Channel channel = rabbitmqUtil.prepareChannel();
            channel.addReturnListener(new RouteFailListener());
            channel.basicPublish(EXCHANGE_NAME,UNKNOWN_ROUTING_KEY,true,null,"你好,我是生产者".getBytes(StandardCharsets.UTF_8));
            log.info("发送消息...");
        } catch (IOException | TimeoutException e) {
            log.error("出现异常: ",e);
        }
    }
}
